<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="taskName">
              <el-input v-model="filters.taskName" clearable placeholder="任务名称"></el-input>
            </el-form-item>

            <el-form-item prop="taskLocation">
              <el-input v-model="filters.taskLocation" clearable placeholder="任务地址"></el-input>
            </el-form-item>

            <el-form-item prop="taskStateId">
              <el-select v-model="filters.taskStateId" clearable placeholder="状态">
                <el-option v-for="item in stateList" :key="item.value" :label="item.lable" :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="taskTypeId">
              <el-select v-model="filters.taskTypeId" clearable placeholder="任务种类">
                <el-option v-for="item in typeList" :key="item.value" :label="item.lable" :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="taskKindId">
              <el-select v-model="filters.taskKindId" clearable placeholder="任务类型">
                <el-option v-for="item in kindList" :key="item.value" :label="item.lable" :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="taskStartDate">
              <el-date-picker v-model="filters.taskStartDate" type="datetime" placeholder="任务开始日期" value-format="yyyy-MM-dd hh:ss:mm" :picker-options="pickerOptions">
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <!-- <el-table-column prop="documentId" label="文档编号" align="center" width="120"></el-table-column > -->
        <el-table-column prop="taskName" label="任务名称" width="150" align="center"></el-table-column>

        <el-table-column prop="taskContent" label="任务内容" width="150" align="center"></el-table-column>

        <el-table-column prop="taskLocation" label="任务地址" width="200"></el-table-column>

        <el-table-column prop="peopleNumber" label="所需人数" width="80"></el-table-column>

        <el-table-column prop="taskStartDate" label="任务开始时间" width="95"></el-table-column>

        <el-table-column prop="taskEndDate" label="任务结束时间" width="95"></el-table-column>

        <el-table-column prop="emolument" label="薪酬"></el-table-column>

        <el-table-column label="紧急程度" prop="busyLevel" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" :type="busyLevelSpanType(scope.row.busyLevel)">
                {{ busyLevelFormat(scope.row.busyLevel) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="taskType" label="任务种类" :formatter="typeFormat" align="center"></el-table-column>

        <el-table-column prop="taskKind" label="任务类型" :formatter="kindFormat" align="center"></el-table-column>

        <el-table-column prop="taskState" label="任务状态" :formatter="taskFormat" align="center"></el-table-column>

        <el-table-column prop="linkPhone" label="联系人手机号" width="130"></el-table-column>

        <el-table-column prop="linkName" label="联系人名称"></el-table-column>

        <el-table-column prop="signStartDate" label="报名开始时间" width="95"></el-table-column>

        <el-table-column prop="signEndDate" label="报名结束时间" width="95"></el-table-column>

        <!-- <el-table-column prop="modifyUserName" label="修改人姓名"></el-table-column>
        <el-table-column prop="modifyDate" label="修改人时间"></el-table-column> -->

        <el-table-column label="操作" width="200" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button v-has="permissionSearch" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-button v-has="permissionEdit" @click.native="handleClickEditor(scope.row)" size="mini">编辑</el-button>
            <el-button v-has="permissionDelete" @click.native="handleUpdateState(scope.row, -1)" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100, 300, 500]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import taskApi from '@/api/sample/task';
import * as message from '@/constants/message';
export default {
  name: 'DocumentInfoManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            },
          },
          {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            },
          },
          {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            },
          },
        ],
      },
      dataList: [],

      stateList: [
        { lable: '待开启', value: 1 },
        { lable: '报名中', value: 2 },
        { lable: '已结束', value: 3 },
      ],
      typeList: [
        { lable: '单采', value: 1 },
        { lable: '混采', value: 2 },
      ],
      kindList: [
        { lable: '人员', value: 1 },
        { lable: '环境', value: 2 },
      ],
      busyLevelList: [
        { lable: '正常', value: 1 },
        { lable: '紧急', value: 2 },
      ],
      filters: {
        //任务名称
        taskName: '',
        //任务地址
        taskLocation: '',
        //任务状态
        taskStateId: '',
        //任务种类
        taskTypeId: '',
        //任务类型
        taskKindId: '',
        //紧急程度
        busyLevelId: '',
        //任务开始时间
        taskStartDate: '',
      },

      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      permissionAdd: 'SYSTEM_MANAGER_SAMPLE_NURSE_TASK_ADD', //添加
      permissionSearch: 'SYSTEM_MANAGER_SAMPLE_NURSE_TASK_DETAIL', // 详情
      permissionEdit: 'SYSTEM_MANAGER_SAMPLE_NURSE_EDIT', // 编辑
      permissionDelete: 'SYSTEM_MANAGER_SAMPLE_NURSE_TASK_DELETE', // 删除
    };
  },
  watch: {
    //to去哪，from从哪来
    $route(to, from) {
      if (from.path == '/sample/sampleNurseTaskAdd' || from.path == '/sample/sampleNurseTaskDetail') {
        this.getData();
      }
    },
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.getData();
    }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      this.getData();
      this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    getData() {
      // 获取列表数据
      let _this = this;

      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        taskName: _this.filters.taskName,
        taskLocation: _this.filters.taskLocation,
        taskState: _this.filters.taskStateId,
        taskType: _this.filters.taskTypeId,
        taskKind: _this.filters.taskKindId,
        busyLevel: _this.filters.busyLevelId,
        taskStartDate: _this.filters.taskStartDate,
      };
      console.log('查询数据', cond);
      taskApi.listTask(cond).then((res) => {
        _this.dataList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
    resetForm() {
      // 重置
      this.$refs.searchForm.resetFields();
      this.init();
    },
    onSearch() {
      // 搜索
      this.page.pageNo = 1;
      this.getData();
    },
    taskFormat(row, column, cellValue) {
      if (cellValue == 1) {
        return '待开启';
      } else if (cellValue == 2) {
        return '报名中';
      } else if (cellValue == 3) {
        return '已结束';
      }
    },
    typeFormat(row, column, cellValue) {
      if (cellValue == 1) {
        return '单采';
      } else if (cellValue == 2) {
        return '混采';
      }
    },
    kindFormat(row, column, cellValue) {
      if (cellValue == 1) {
        return '人员';
      } else if (cellValue == 2) {
        return '环境';
      }
    },
    busyLevelSpanType(cellValue) {
      if (cellValue == 2) {
        return 'warning';
      } else if (cellValue == 1) {
        return 'success';
      }
    },
    busyLevelFormat(cellValue) {
      if (cellValue == 1) {
        return '正常';
      } else if (cellValue == 2) {
        return '紧急';
      }
    },
    handleClickAdd() {
      // 新增
      let _this = this;
      _this.$router.push('/sample/sampleNurseTaskAdd');
    },
    handleClickEditor(row) {
      // 编辑
      let _this = this;
      _this.$router.push({
        path: '/sample/sampleNurseTaskAdd',
        query: { id: row.taskId },
      });
    },
    handleClickDetail(row) {
      // 查看详情
      let _this = this;
      _this.$router.push({
        path: '/sample/sampleNurseTaskDetail',
        query: { id: row.taskId },
      });
    },
    handleUpdateState(row, state) {
      // 修改所有状态
      let _this = this;
      let cond = {
        id: row.taskId,
        state: state,
      };
      _this.$confirm('确认删除该任务？', message.TITLE_PROMPT).then(() => {
        taskApi.delTask(cond).then((res) => {
          this.$Utils.notifySuccess();
          this.getData();
        });
      });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.getData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.getData();
    },
  },
};
</script>

<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
